package com.wframe.project.former.dataservices.order;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.frame.database.JBaseService;
import com.frame.database.JServiceFactory;
import com.frame.database.JdataBean;
import com.frame.database.PageResult;
import com.frame.util.DataConvert;
import com.wframe.project.former.dataservices.products.dao.ProductClassDao;
import com.wframe.project.former.dataservices.upload.UploadDao;
import com.wframe.project.former.enums.OrderStatusEnum;
import com.wframe.project.former.enums.OrderSynEnum;

public class OrderService extends JBaseService<OrderDao>{

	public static OrderService getIntance(){
        return JServiceFactory.createJServiceProxy(OrderService.class);
    }

	public PageResult queryOrder(JdataBean parms,int size,int pageno) throws Exception{
		return  getDao().queryOrder(parms,size,pageno);
	}
	
	public JdataBean getOrderInfo(String id) throws Exception{
		JdataBean order=getDao().getOrder(id);
		List<JdataBean> products=getDao().queryOrderProduct(id);
		ProductClassDao productsDao=new ProductClassDao();
		for(JdataBean product:products){
			product.put("classname", productsDao.getProductClassName(product.getString("productid")));
		}
		order.put("products", products);

		return order;
	}
	public JdataBean getBackOrderInfo(String id) throws Exception{
		JdataBean order=getDao().getOrder(id);
		JdataBean backorder=getDao().getBackOrder(id);
		order.put("applytime", backorder.get("applytime"));
		order.put("backphone", backorder.get("phone"));
		order.put("reason", backorder.get("reason"));
		order.put("authtime", backorder.get("authtime"));
		order.put("authcon", backorder.get("authcon"));
		if(StringUtils.isNotBlank(backorder.getString("photodataid"))){
			//退货图片
			order.put("backphotos", new UploadDao().queryPhotos(backorder.getString("photodataid")));
		}
		List<JdataBean> orderproducts=getDao().queryOrderProduct(id);
		List<JdataBean> backproducts=getDao().queryBackOrderProduct(id);
		double summoney=0;
		for(JdataBean product:backproducts){
			for(JdataBean oproduct:orderproducts){
				if(oproduct.getString("id").equals(product.getString("priceid"))){
					product.put("buyamount", oproduct.get("buyamount"));
					break;
				}
			}
			summoney=DataConvert.doubleSum(summoney,product.getDouble("sumprice"));
		}
		order.put("products", backproducts);
		order.put("backmoney", summoney);
		return order;
	}
	public void fahuo(String orderid,String send_time,String expres_type,String expres_code) throws Exception{
		getDao().fahuo(orderid, send_time, expres_type, expres_code);
	}
	
	public PageResult queryBackOrder(JdataBean parms,int size,int pageno) throws Exception{
		return  getDao().queryBackOrder(parms,size,pageno);
	}
	
	/**
	 * 退货审核
	 * @param authstatus：0拒绝、1同意
	 * @param authcon
	 * @throws Exception 
	 */
	public void backorderauth(String orderid,int authstatus,String authcon) throws Exception{
		int status=2;
		if(authstatus==0)status=3;
		getDao().updateorderBackTag(orderid, status);
		getDao().backorderauth(orderid, status,authcon);
	}
	public void orderconfirm(String orderid) throws Exception{
		Map key=new HashMap();
		key.put("id", orderid);
		Map upmap=new HashMap();
		upmap.put("status", OrderStatusEnum.NoSend.getStatus());
		upmap.put("issyn", OrderSynEnum.NoSyn.getStatus());
		getDao().update(upmap, key);
	}
}
